package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qs.constant.NoticeStatus;
import com.qs.constant.RedisConstant;
import com.qs.pojo.dto.NoticeDto;
import com.qs.pojo.entity.Notice;
import com.qs.mapper.NoticeMapper;
import com.qs.pojo.vo.NoticeVo;
import com.qs.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lettuce.core.cluster.models.partitions.RedisClusterNode;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 前后台公告表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-12-16
 */
@Service
@RequiredArgsConstructor
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    private final RedisTemplate redisTemplate;

    @Override
    public void saveNotice(Notice notice) {
        if (notice.isNoticeStatus()){
            notice.setStatus(NoticeStatus.PUBLISHED);
        }
        baseMapper.insert(notice);
        redisTemplate.delete(RedisConstant.INDEX_NOTICE);
    }

    @Override
    public Map<String, Object> getNoticeList(Integer pageNo, Integer pageSize, NoticeVo noticeVo) {
        String trimTitle = noticeVo.getTitle().trim();
        Page<Notice> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isBlank(trimTitle),"title",trimTitle);
        Page<Notice> noticePage = baseMapper.selectPage(pageInfo, wrapper);
        if(null != noticePage){
            Map<String,Object> map = new HashMap<>();
            List<Notice> noticeList = noticePage.getRecords();
            List<NoticeDto> list = new ArrayList<>();
            if(!CollectionUtils.isEmpty(noticeList)){
                noticeList.stream().forEach(notice -> {
                    NoticeDto noticeDto = new NoticeDto();
                    BeanUtils.copyProperties(notice, noticeDto);
                    noticeDto.setKey(notice.getId());
                    list.add(noticeDto);
                });
            }
            if(!CollectionUtils.isEmpty(noticeList)){
                map.put("total", noticePage.getTotal());
                map.put("data", list);
                return map;
            }
            map.put("total", noticePage.getTotal());
            map.put("data", null);
            return map;
        }
        return null;
    }

    @Override
    public void updateNotice(Notice notice) {
        if (notice.isNoticeStatus()){
            notice.setStatus(NoticeStatus.PUBLISHED);
        }else{
            notice.setStatus(NoticeStatus.NO_PUBLISHED);
        }
        baseMapper.updateById(notice);
        redisTemplate.delete(RedisConstant.INDEX_NOTICE);
    }

    @Override
    public Notice echo(Integer id) {
        if(id != null){
            Notice notice = baseMapper.selectById(id);
            if(notice != null){
                if(notice.getStatus().intValue() == NoticeStatus.PUBLISHED){
                    notice.setNoticeStatus(true);
                }
                notice.setKey(notice.getId());
                return notice;
            }
            return null;
        }
        return null;
    }

    @Override
    public List<NoticeDto> getNoticeFrontList() {
        List<NoticeDto> redisNotices = (List<NoticeDto>) redisTemplate.opsForValue().get(RedisConstant.INDEX_NOTICE);
        if(!CollectionUtils.isEmpty(redisNotices)){
            return redisNotices;
        }
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("flag",NoticeStatus.FRONT_NOTICE);
        wrapper.eq("status",NoticeStatus.PUBLISHED);
        List<Notice> notices = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(notices)){
            List<NoticeDto> list = new ArrayList<>();
            notices.stream().forEach(notice -> {
                NoticeDto noticeDto = new NoticeDto();
                BeanUtils.copyProperties(notice, noticeDto);
                noticeDto.setKey(notice.getId());
                list.add(noticeDto);
            });
            redisTemplate.opsForValue().set(RedisConstant.INDEX_NOTICE,list);
            return list;
        }
        return null;
    }
}
